/**
 * 
 */
package com.exject.android.socialphonebook.view;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.AdapterView.OnItemClickListener;

import com.exject.android.socialphonebook.R;
import com.exject.android.socialphonebook.data.Contact;
import com.exject.android.socialphonebook.util.RetrieveContactHelper;

/**
 * @author Benjamin Nagel <benjamin.nagel@stud.fh-dortmund.de>
 */
public class ContactsListActivity extends ListActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        final Cursor c = this.getContentResolver().query(
                Contacts.People.CONTENT_URI, null, null, null, null);
        this.startManagingCursor(c);
        final ContactListAdapter adapter = new ContactListAdapter(this, c);
        this.setListAdapter(adapter);
        this.getListView().setOnItemClickListener(new OnItemClickListener() {
            
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view,
                    int position, long id) {
                final String name = ContactListAdapter.getNameFromElement(view);
                final RetrieveContactHelper retrieveContactHelper = new RetrieveContactHelper(
                        ContactsListActivity.this.getContentResolver());
                
                final Contact foundContact = retrieveContactHelper
                        .getContactByName(name);
                Intent displayContact = new Intent(ContactsListActivity.this,
                        DisplayContactOverviewActivity.class);
                displayContact.putExtra("contactId", foundContact.getId());
                startActivity(displayContact);

            }
        });
    }
}
